package com.zmc.member.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.member.infrastructure.dao.MemberAuthEmailDao;
import com.zmc.member.infrastructure.entity.MemberAuthEmailEntity;
import com.zmc.member.infrastructure.service.MemberAuthEmailService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("memberAuthEmailService")
public class MemberAuthEmailServiceImpl extends ServiceImpl<MemberAuthEmailDao, MemberAuthEmailEntity> implements MemberAuthEmailService {

    @Override
    public MemberAuthEmailEntity queryByEmail(String account) {
        LambdaQueryWrapper<MemberAuthEmailEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberAuthEmailEntity::getEmail, account)
                .last(" limit 1");
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void updateEmail(String memberId, String email, String newEmail) {
        baseMapper.removeByEmail(email);
        MemberAuthEmailEntity entity = new MemberAuthEmailEntity();
        entity.setMemberId(memberId);
        entity.setEmail(newEmail);
        save(entity);
    }

}